<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<title>二十二部资产管理系统</title>
	<script src="/ams22/s/JS/quagga.js"></script>
	<script src="/ams22/s/layui/layui1.js"></script>
	<style type="text/css">
		.viewport canvas, video{
			float: left;
			max-width: 100%;
			width: 100%;
			height: 100vw;
		}
		.viewport video{
			z-index: 100;
		}
		.viewport canvas.drawingBuffer, video.drawingBuffer{
			margin-left: -100%;
		}
	</style>
</head>
<body class="layui-layout-body">
	<h5>扫码测试1</h5>
	<h5 id="msg"></h5>
	<button onclick="openScan()">点击扫码</button>
	<script type="text/javascript">
		var layer,$;

		layui.use(['layer', 'jquery'],function(){
			layer = layui.layer;
			$ = layui.$;
		});

		// 打开扫描窗口
		function openScan(){
			layer.open({
				type: 1,
				title: '扫码框',
				content: $('#scanLayout'),
				resize: false,
				move: false,
				area: '100%',
				offset: '10%',
				shade: 0.3,
				success: function(layero, index){
					layer.load(2,{
						shade: 0.3,
						success: function(layero, indexL){
							
							/**
							 * 扫描核心代码
							 * */

							try{
								// 构造
								Quagga.init({
									// 输入流
									inputStream: {
										name: 'Live',
										type: 'LiveStream',
										constraints: {
											// 摄像头分辨率
											width: { min: 1080 },
											height: { min: 1920 },
											// 摄像头方向
											facingMode: 'environment'
										},
										// 扫码识别范围
										area: {
											top: '20%',
											bottom: '20%',
											left: '5%',
											right: '5%',
										},
										target: document.getElementById('canvanCode'),
									},
									// 解码种类
									decoder: {
										readers: ['code_128_reader', '2of5_reader'],
										multiple: false
									},
									locator: {
										pathSize: 'small',
										halfSample: true
									}
								}, 
								// 构造完成回调方法
								function(err){
									if (err) {
										layer.close(indexL);
										layer.msg(err.message);
										return;
									}
									
									let track = Quagga.CameraAccess.getActiveTrack();
									let capabilities = {};
									if (typeof track.getCapabilities == 'function') {
										capabilities = track.getCapabilities();
									}
									
									// 设置解码成功回调方法
									Quagga.onDetected(function(data){
										document.getElementById('msg').innerHTML = data.codeResult.code;
										layer.close(index);
										// window.alert(data.codeResult.code);
									});
									// Quagga.onProcessed(function(result) {
									// 	var drawingCtx = Quagga.canvas.ctx.overlay,
									// 		drawingCanvas = Quagga.canvas.dom.overlay;

									// 	if (result) {
									// 		if (result.boxes) {
									// 			drawingCtx.clearRect(0, 0, parseInt(drawingCanvas.getAttribute("width")), parseInt(drawingCanvas.getAttribute("height")));
									// 			result.boxes.filter(function (box) {
									// 				return box !== result.box;
									// 			}).forEach(function (box) {
									// 				Quagga.ImageDebug.drawPath(box, {x: 0, y: 1}, drawingCtx, {color: "green", lineWidth: 2});
									// 			});
									// 		}

									// 		if (result.box) {
									// 			Quagga.ImageDebug.drawPath(result.box, {x: 0, y: 1}, drawingCtx, {color: "#00F", lineWidth: 2});
									// 		}

									// 		if (result.codeResult && result.codeResult.code) {
									// 			Quagga.ImageDebug.drawPath(result.line, {x: 'x', y: 'y'}, drawingCtx, {color: 'red', lineWidth: 3});
									// 		}
									// 	}
									// });

									// 启动扫码
									Quagga.start();
									// 因为设定了扫码识别的范围，所以在最上层添加一个简易扫码框
									createScanBox();

									// let layout = document.getElementById('scanLayout');
									// let canvan = document.getElementById('canvanCode');

									// document.getElementById('childElement').innerHTML += layout.childElementCount;
									// document.getElementById('canvanElement').innerHTML += canvan.childElementCount;
									// document.getElementById('fEl').innerHTML += canvan.children[0].tagName;
									// document.getElementById('sEl').innerHTML += canvan.children[1].tagName;
									// document.getElementById('tEl').innerHTML += canvan.children[2].tagName;

									layer.close(indexL);
								});
							}catch(err){
								layer.close(indexL);
								layer.msg(err.message);
							}

							/**
							 * 核心代码结束
							 * */
						}
					})
				},
				end: function(layero, index){
					// 停止扫码
					Quagga.stop();
				}
			});
		}

		// 通过canvas绘制扫码框
		function createScanBox(){
			try{
				let myCanvas = document.getElementById('my-canvas');
				let bw = myCanvas.offsetWidth;
				let bh = myCanvas.offsetHeight;
				myCanvas.height = bh;
				myCanvas.width = bw;
				let ctx = myCanvas.getContext('2d');
				let t = bh*0.2, m=bh*0.5-2, b = bh*0.8, l=bw*0.05, r=bw*0.95;

				console.log(bw+'_'+bh);
				console.log(t+'_'+m+'_'+b+'_'+l+'_'+r);

				ctx.clearRect(0, 0, bw, bh);
				ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
				ctx.fillRect(0, 0, bw, bh);
				ctx.clearRect(l, t, r-l, b-t);

				l += 15;
				t += 15;
				r -= 15;
				b -= 15;

				ctx.fillStyle = '#ffdd36';
				ctx.fillRect(l, t, r-l, b-t);
				ctx.clearRect(l+10, t+10, r-l-20, b-t-20);
				ctx.clearRect(l+30, t-5, r-l-60, b-t+10);
				ctx.clearRect(l-5, t+30, r-l+10, b-t-60);

				ctx.fillStyle = '#e25b5b';
				ctx.fillRect(l, m, r-l, 4);
			}catch(err){
				layer.msg(err.message);
			}
		}
		
	</script>

	<!-- 悬浮窗 -->
	<div id="scanLayout" style="display: none;">
		<div id="canvanCode" class="viewport"></div>
		<canvas id="my-canvas" style="float: left; margin-top: -100vw; width: 100%; height: 100vw; z-index: 9999;"></canvas>
	</div>
</body>
</html>